<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            关键件条码检测
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label">关键件条码</div>
                    <div class="aui-list-item-input">
                        <input type="text" placeholder="请 扫描/输入 关键件条码" id="consignment" value="" onclick="getIdSelector();">
                    </div>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <!-- <div class="aui-btn aui-btn-info" style="width:30%;margin-left:2%;" onclick="doScan()">一键解析</div> -->

        <div style="background-color:white; width: 100%;margin-top:2%;white-space: nowrap;overflow-x: auto;-webkit-overflow-scrolling:touch;">
            <table class="table" style='width:1000px;' id="table1">
                <tbody style="display:block">
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 15px;font-weight: bold;">
                        <td algin='center' style="width:40px;"></td>
                        <td algin='center' style="width:50px;">序号</td>
                        <td algin='center' style="width:120px;">物料号</td>
                        <td algin='center' style="width:160px;">物料名称</td>
                        <td algin='center' style="width:100px;">供应商编码</td>
                        <td algin='center' style="width:200px;">供应商名称</td>
                        <td algin='center' style="width:130px;">批次号</td>
                        <td algin='center' style="width:100px;">流水号</td>
                        <td algin='center' style="width:80px;">创建人</td>
                        <td algin='center' style="width:165px;;">创建时间</td>
                    </tr>
                </tbody>
            </table>
            <table class="table" style='width:1000px;height:800px' id="table">
                <tbody id="tbodyHtml" style="display: block;height: 300px;overflow-y: scroll;">
                </tbody>
            </table>
        </div>
    </div>
    <div>
        <div style="height:60px;"></div>
        <footer class="aui-bar aui-bar-tab" id="footer">
            <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
                <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
            </div>
        </footer>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";

    //初始化查询仓库下拉框
    apiready = function() {

        $("#consignment").focus();
        idSelector = "consignment";

    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //一键发货
    function createUqcmChkBill() {
        //发货单号
        var consignment = $("#consignment").val();

        //发货单号非空校验
        if (consignment == "" || consignment == null || consignment == undefined) {
            api.toast({
                msg: "发货单号不能为空",
                duration: 3000
            });
            return;
        }

        api.showProgress({
            title: '处理中...',
        });

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "appUwmController!oneKeyDelivery.m";
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';
        var data = {
            values: {
                consignment: consignment, //发货单号
                un: un
            }
        };
        ajaxForData(params, data, false, function(ret, err) {
            if (ret) {
                var myobj = eval("(" + ret.data + ")");

                //成功
                if (myobj.code == "0") {

                    api.hideProgress();
                    api.toast({
                        msg: "一键发货成功！",
                        duration: 3000
                    });
                    //刷新表格
                    searchDetails(consignment);

                    //失败,模板存在性校验
                } else if (myobj.code == "1") {
                    api.hideProgress();
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                }
            } else {
                api.hideProgress();
            }
        });
    }
    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {

            //1：发货单号
            if (idSelector == 'consignment') {

                //发货单号
                $("#consignment").val(ret);

                //查询表格数据
                searchDetails(ret);

            }
        });
    }

    //发货单监听回车事件
    $("#consignment").on('keypress', function(e) {
        var keycode = e.keyCode;
        var consignment = $(this).val();
        if (keycode == '13') {

            //查询表格数据
            searchDetails(consignment);
        }
    });

    //发货单号值改变
    function onChangeConsignment() {
        //取发货单号
        var consignment = $("#consignment").val();
        //发货单为空，清空表格信息
        if (consignment == "" || consignment == null || consignment == undefined) {

            $api.html($api.byId("tbodyHtml"), "");

            //光标
            $("#consignment").focus();
            idSelector = "consignment";

            //不为空，
        } else {

            //查询表格数据
            searchDetails(consignment);
        }
    }

    //根据发货单查询表格数据
    function searchDetails(consignment) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/scanKeyBarcode";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                barcode: consignment,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                $("#consignment").val("");
                //返回成功
                if (myobj.code == "0") {

                    var list = myobj.list;
                    var html = "";

                    console.log(list);
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;background-color: #F5F5F5;color: black;font-size: 12px;'>";
                        html += "<td style='width:40px;'><input name='sub' type='checkbox' /></td>";
                        html += "<td name='seqNo' style='width:50px;'>" + list[i].seqNo + "</td>";
                        html += "<td name='boxnum' style='width:130px;'>" + list[i].mrlcode + "</td>";
                        html += "<td name='mrl_name' style='width:150px;'>" + list[i].mrlname + "</td>";
                        html += "<td name='invoice_status' style='width:100px;'>" + list[i].suppliercode + "</td>";
                        html += "<td name='invoice_statuss' style='width:200px;'>" + list[i].suppliername + "</td>";
                        html += "<td name='mrl_code' style='width:130px;'>" + list[i].lotcode + "</td>";
                        html += "<td name='lot_code' style='width:110px;'>" + list[i].sortcode + "</td>";
                        html += "<td name='send_qyt' style='width:90px;'>" + list[i].create_id + "</td>";
                        html += "<td name='consignment' style='width:130px;'>" + list[i].create_date + "</td>";
                        html += "</tr>";
                    }



                    $api.html($api.byId("tbodyHtml"), html);

                    //失败
                } else if (myobj.code == "1") {

                    $("#consignment").select();
                    idSelector = "consignment";
                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //页面刷新
    function refresh(consignment) {
        searchDetails(consignment);
    }


    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
